import SwiftUI

struct SettingsView: View {
    @StateObject var viewModel = SettingsViewModel()
    @State private var showingSavedAlert = false
    
    var body: some View {
        NavigationStack {
            Form {
                // 饮水目标设置
                Section(header: Text("饮水目标")) {
                    HStack {
                        Text("每日目标")
                        Spacer()
                        TextField("目标", value: $viewModel.userProfile.goal, format: .number)
                            .keyboardType(.decimalPad)
                            .multilineTextAlignment(.trailing)
                        Text(viewModel.getCurrentUnitText())
                    }
                }
                
                // 提醒设置
                Section(header: Text("提醒设置")) {
                    HStack {
                        Text("提醒间隔")
                        Spacer()
                        Picker("", selection: $viewModel.reminderSettings.interval) {
                            ForEach([30, 60, 120, 180], id: \.self) { minutes in
                                Text(formatInterval(minutes)).tag(minutes)
                            }
                        }
                        .pickerStyle(MenuPickerStyle())
                    }
                    
                    DatePicker(
                        "开始时间",
                        selection: $viewModel.reminderSettings.startTime,
                        displayedComponents: .hourAndMinute
                    )
                    
                    DatePicker(
                        "结束时间",
                        selection: $viewModel.reminderSettings.endTime,
                        displayedComponents: .hourAndMinute
                    )
                    
                    Toggle("提醒声音", isOn: $viewModel.reminderSettings.isSoundEnabled)
                    
                    Toggle("振动", isOn: $viewModel.reminderSettings.isVibrationEnabled)
                }
                
                // 单位设置
                Section(header: Text("单位设置")) {
                    Toggle("使用毫升 (ml)", isOn: $viewModel.isMetricUnit)
                        .onChange(of: viewModel.isMetricUnit) { newValue in
                            // 当单位更改时，转换当前目标值
                            viewModel.userProfile.goal = viewModel.convertValue(
                                value: viewModel.userProfile.goal,
                                toMetric: newValue
                            )
                        }
                }
                
                // 保存按钮
                Section {
                    Button("保存设置") {
                        viewModel.saveSettings()
                        showingSavedAlert = true
                    }
                    .frame(maxWidth: .infinity, alignment: .center)
                    .foregroundColor(.blue)
                }
            }
            .navigationTitle("设置")
            .alert("设置已保存", isPresented: $showingSavedAlert) {
                Button("确定", role: .cancel) { }
            } message: {
                Text("你的设置已成功保存，提醒将根据新设置更新。")
            }
        }
    }
    
    private func formatInterval(_ minutes: Int) -> String {
        if minutes < 60 {
            return "\(minutes)分钟"
        } else {
            let hours = minutes / 60
            return "\(hours)小时"
        }
    }
} 